package com.landinfo.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.landinfo.service.ResourcesSurveyService;
import com.landinfo.service.dto.ResourcesSurveyDTO;
import com.landinfo.web.rest.util.HeaderUtil;
import io.github.jhipster.web.util.ResponseUtil;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * 资源管理
 */
@RestController
@RequestMapping("/api")
public class ResourcesSurveyResource {

    private final Logger log = LoggerFactory.getLogger(BookMarkResource.class);

    private static final String ENTITY_NAME = "bookMark";

    private final ResourcesSurveyService resourcesSurveyService;

    public ResourcesSurveyResource(ResourcesSurveyService resourcesSurveyService) {
        this.resourcesSurveyService = resourcesSurveyService;
    }

    @PostMapping("/resources-survey")
    @Timed
    public ResponseEntity<ResourcesSurveyDTO> createResourcesSurvey(@Valid @RequestBody ResourcesSurveyDTO resourcesSurveyDTO) throws URISyntaxException {
        log.debug("REST request to save resourcesSurveyDTO : {}", resourcesSurveyDTO);
        if (resourcesSurveyDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new resourcesSurveyDTO cannot already have an ID")).body(null);
        }
        ResourcesSurveyDTO result = resourcesSurveyService.save(resourcesSurveyDTO);
        return ResponseEntity.created(new URI("/api/book-marks/" + result.getId()))
                .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
                .body(result);
    }

    /**
     * PUT  /book-marks : Updates an existing bookMark.
     *
     * @param resourcesSurveyDTO the bookMarkDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated bookMarkDTO,
     * or with status 400 (Bad Request) if the bookMarkDTO is not valid,
     * or with status 500 (Internal Server Error) if the bookMarkDTO couldnt be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/resources-survey")
    @Timed
    public ResponseEntity<ResourcesSurveyDTO> updateResourcesSurvey(@Valid @RequestBody ResourcesSurveyDTO resourcesSurveyDTO) throws URISyntaxException {
        log.debug("REST request to update ResourcesSurvey : {}", resourcesSurveyDTO);
        if (resourcesSurveyDTO.getId() == null) {
            return createResourcesSurvey(resourcesSurveyDTO);
        }
        ResourcesSurveyDTO result = resourcesSurveyService.save(resourcesSurveyDTO);
        return ResponseEntity.ok()
                .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, resourcesSurveyDTO.getId().toString()))
                .body(result);
    }


    /**
     * GET  /book-marks : get all the bookMarks.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of bookMarks in body
     */
    @GetMapping("/resources-survey")
    @Timed
    public List<ResourcesSurveyDTO> getAllResourcesSurveys() {
        log.debug("REST request to get all ResourcesSurveys");
        return resourcesSurveyService.findAll();
    }

    /**
     * GET  /book-marks : get all the bookMarks.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of bookMarks in body
     */
    @GetMapping("/resources-survey/getYear")
    @Timed
    public List<Object> getAllYears() {
        log.debug("REST request to get all ResourcesSurveys");
        List<Object> result = resourcesSurveyService.getYears();
        return result;
    }

    /**
     * GET  /book-marks/:id : get the "id" bookMark.
     *
     * @param id the id of the bookMarkDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the bookMarkDTO, or with status 404 (Not Found)
     */
    @GetMapping("/resources-survey/getByXzqhParentCode/{id}")
    @Timed
    public ResponseEntity<List<ResourcesSurveyDTO>> findByXzqhParentCode(@PathVariable String id) {
        log.debug("REST request to get ResourcesSurvey : {}", id);
        List<ResourcesSurveyDTO> bookMarkDTO = resourcesSurveyService.findByXzqhParentCode(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(bookMarkDTO));
    }

    @GetMapping("/resources-survey/getByXzqhCode/{xzqhCode}/{year}/{regionType}")
    @Timed
    public ResponseEntity<List<ResourcesSurveyDTO>> getByXzqhCode(@PathVariable String xzqhCode, @PathVariable Integer year, @PathVariable String regionType) {
        log.debug("REST request to get ResourcesSurvey : {}", xzqhCode);
        List<ResourcesSurveyDTO> resourcesSurveyDTOs = resourcesSurveyService.findByXzqhCode(xzqhCode, year, regionType);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(resourcesSurveyDTOs));
    }


    /**
     * DELETE  /book-marks/:id : delete the "id" bookMark.
     *
     * @param id the id of the bookMarkDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/resources-survey/{id}")
    @Timed
    public ResponseEntity<Void> deleteResourcesSurvey(@PathVariable String id) {
        log.debug("REST request to delete ResourcesSurvey : {}", id);
        resourcesSurveyService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

}
